package com.firmail.exception.type;

import com.firmail.enumeration.MockParamsEnum;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Getter
public enum ExceptionType implements MockParamsEnum {
    /**
     * 系统错误
     */
    SYSTEM_EXCEPTION(Constants.EC_INTERNAL_ERROR, Constants.EC_INTERNAL_ERROR_DESC),
    /**
     * 参数错误
     */
    PARAMETER_ERROR_EXCEPTION(Constants.PARAMETER_ERROR, Constants.PARAMETER_ERROR_DESC),
    MEDIATYPE_NOT_SURPPORTED(Constants.EC_MEDIATYPE_NOT_MATCHED,Constants.EC_MEDIATYPE_NOT_MATCHED_DESC),
    /**
     * 非法参数错误
     */
    INVALID_PARAMETER_EXCEPTION(Constants.INVALID_PARAMETER_ERROR, Constants.INVALID_PARAMETER_ERROR_DESC),
    /**
     * 参数无效
     */
    PARAMETER_INVALID_EXCEPTION(Constants.PARAMETER_INVALID_ERROR,Constants.PARAMETER_INVALID_ERROR_DESC),
    /**
     * 用户锁定
     */
    ACCOUNT_LOCK_ERROR(Constants.EC_ACCOUNT_LOCK_ERROR,Constants.EC_ACCOUNT_LOCK_ERROR_DESC),
    /**
     * 连续登录错误3次
     */
    LOGIN_FAIL_THIRD(Constants.EC_LOGIN_FAIL_3_ERROR, Constants.EC_LOGIN_FAIL_3_ERROR_DESC),
    /**
     * 登录验证码错误
     */
    LOGIN_CODE_ERROR_EXCEPTION(Constants.EC_LOGIN_CODE_ERROR,Constants.EC_LOGIN_CODE_ERROR_DESC);

    private Integer code;

    private String message;

    ExceptionType(Integer code, String message) {
        this.code=code;
        this.message= message;
    }

    /**
     * 根据code获取名字
     */
    public static String getName (Integer code){

        for (ExceptionType et: ExceptionType.values()) {
            if ( code==et.getCode())
                return et.message;
        }
        return null;
    }

    /**
     * 根据value返回枚举类型,主要在switch中使用
     */
    public static ExceptionType getByCode(Integer code){
        for (ExceptionType et:ExceptionType.values()) {
            if (code==et.getCode())
                return et;
        }
        return  null;
    }


    @Override
    public void print() {
        log.info("{} :{}",this.code, this.message);
    }

    @Override
    public String getInfo() {
        return getMessage();
    }
}
class Constants{

    // 系统错误
    public static final Integer EC_INTERNAL_ERROR = 1001;
    public static final String EC_INTERNAL_ERROR_DESC = "[服务器异常] ";
    // token错误
    public static final Integer EC_INVALID_TOKEN = 1101;
    public static final String EC_INVALID_TOKEN_DESC = "[token错误] ";

    public static final Integer EC_MEDIATYPE_NOT_MATCHED = 1211;
    public static final String EC_MEDIATYPE_NOT_MATCHED_DESC = "[请求类型不支持] ";
    // 参数错误
    public static final Integer PARAMETER_ERROR = 1300;
    public static final String PARAMETER_ERROR_DESC = "[参数错误] ";
    /**
     * 参数为空错误
     */
    public static final Integer INVALID_PARAMETER_ERROR = 1301;
    public static final String INVALID_PARAMETER_ERROR_DESC = "[非法参数错误] ";
    // 参数无效
    public static final Integer PARAMETER_INVALID_ERROR = 1302;
    public static final String PARAMETER_INVALID_ERROR_DESC = "[参数无效] ";
    // 没有权限
    public static final Integer EC_NOT_AUTHORITY = 1320;
    public static final String EC_NOT_AUTHORITY_DESC = "[没有权限] ";
    // json 错误
    public static final Integer EC_INVALID_JSON = 1401;
    public static final String EC_INVALID_JSON_DESC = "[json格式错误] ";

    public static final Integer EC_FILE_NOT_EXISTS = 1420;
    public static final String EC_FILE_NOT_EXISTS_DESC = "[文件不存在或已删除] ";

    public static final Integer EC_LOGIN_CODE_ERROR = 1601;
    public static final String EC_LOGIN_CODE_ERROR_DESC = "[验证码错误] ";

    public static final Integer EC_ACCOUNT_ERROR = 1602;
    public static final String EC_ACCOUNT_ERROR_DESC = "[手机号错误] ";

    public static final Integer EC_FREQUENT_SEND_CODE_ERROR = 1603;
    public static final String EC_FREQUENT_SEND_CODE_ERROR_DESC = "[验证码发送频繁] ";

    public static final Integer EC_VERIFICATION_FAIL_3_ERROR = 1604;
    public static final String EC_VERIFICATION_FAIL_3_ERROR_DESC = "[验证码输错3次] ";

    public static final Integer EC_LOGIN_FAIL_3_ERROR = 1605;
    public static final String EC_LOGIN_FAIL_3_ERROR_DESC = "[连续登录错误3次] ";

    public static final Integer EC_VERIFICATION_SEND_FAIL_ERROR = 1607;
    public static final String EC_VERIFICATION_SEND_FAIL_ERROR_DESC = "[验证码发送失败] ";

    public static final Integer EC_ACCOUNT_LOCK_ERROR = 2001;
    public static final String EC_ACCOUNT_LOCK_ERROR_DESC = "[用户锁定中] ";

    public static final Integer EC_CANCEL_MESSAGE_TIMEOUT_ERROR = 2002;
    public static final String EC_CANCEL_MESSAGE_TIMEOUT_ERROR_DESC = "[消息超过了2分钟的限制.] ";
}